package com.example.chessbattleplatform.web.chess_rooms.Pojo.websocket;

import com.example.chessbattleplatform.Pojo.WebSocketSessionIdAndUserId;
import com.example.chessbattleplatform.utils.WebSocketSessionSendMessage;

import com.example.chessbattleplatform.web.chess_rooms.Pojo.Vo.RoomsDataVo;
import com.example.chessbattleplatform.web.chess_rooms.service.impl.RoomsServiceInterface;
import jakarta.websocket.Session;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class QueryInformationAboutUserCurrentRoom  extends WebSocketSessionSendMessage {

    // 群发 房间数据
    public void sendQueriesMessages(RoomsServiceInterface roomsService) {
        Map<String, WebSocketSessionIdAndUserId> sessionMap = super.getSessionMap();
        log.info(" 群发 发送列表房间数据: {}", sessionMap);
        // 遍历 sessionMap
        // key: sessionId
        // value: WebSocketSessionIdAndUserId -> userId , session
        sessionMap.forEach((key, value) -> {
            // 查询用户当前房间信息
            try {
                RoomsDataVo room = roomsService.getRoom(value.getUserId());
                super.sendSuccessMessage(room, value.getSession());
            }catch (Exception e){
                log.error("查询用户当前房间信息失败: {}", e.getMessage());
            }
        });

    }

    // 指定发送 id 并发送,列表房间数据
    public void sendQueriesMessage(Session session, RoomsServiceInterface roomsService) {
        //获取
        Long userId = super.getSessionMap().get(session.getId()).getUserId();
        // 查询用户当前房间信息
        RoomsDataVo room = roomsService.getRoom(userId);
        log.info(" 发送列表房间数据: {}", room);
        //发送
        super.sendSuccessMessage(room, session);
    }
}
